package old;


import java.util.ArrayList;
import java.util.Iterator;

import ncsa.hdf.object.h5.H5File;
import static java.lang.System.out;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author SimplyYogi
 */
public class Track {

    private String trackID = "";
    private String title = "";
    private String path = "";
    private double tempo = 0.0;
    private double loudness = 0.0;
    private double[] segments_timbre_mean = new double[12];
    private double[] segments_timbre_var = new double[12];
    private double[] segments_pitch_mean = new double[12];
    private double[] segments_pitch_var = new double[12];
    private String basePath = "F:/MillionSong/Extracted/"; //default path for track location


    public Track(String trackID) {
        this.trackID = trackID;
        String location = trackID.charAt(2) + "/" + trackID.charAt(3) + "/" + trackID.charAt(4) + "/";
        this.path = basePath + location;
    }

    public Track(String path, String trackID) {
        this.trackID = trackID;
        this.basePath = path;
        String location = trackID.charAt(2) + "/" + trackID.charAt(3) + "/" + trackID.charAt(4) + "/";
        this.path = basePath + location;
    }

    /**
     * @return the segments_timbre_mean
     */
    public double[] getSegments_timbre_mean() {
        return segments_timbre_mean;
    }

    /**
     * @param segments_timbre_mean the segments_timbre_mean to set
     */
    public void setSegments_timbre_mean(double[] segments_timbre_mean) {
        this.segments_timbre_mean = segments_timbre_mean;
    }

    /**
     * @return the segments_timbre_var
     */
    public double[] getSegments_timbre_var() {
        return segments_timbre_var;
    }

    /**
     * @param segments_timbre_var the segments_timbre_var to set
     */
    public void setSegments_timbre_var(double[] segments_timbre_var) {
        this.segments_timbre_var = segments_timbre_var;
    }

    /**
     * @return the segments_pitch_mean
     */
    public double[] getSegments_pitch_mean() {
        return segments_pitch_mean;
    }

    /**
     * @param segments_pitch_mean the segments_pitch_mean to set
     */
    public void setSegments_pitch_mean(double[] segments_pitch_mean) {
        this.segments_pitch_mean = segments_pitch_mean;
    }

    /**
     * @return the segments_pitch_var
     */
    public double[] getSegments_pitch_var() {
        return segments_pitch_var;
    }

    /**
     * @param segments_pitch_var the segments_pitch_var to set
     */
    public void setSegments_pitch_var(double[] segments_pitch_var) {
        this.segments_pitch_var = segments_pitch_var;
    }

    public void loadCharacteristics() {
        H5File h5 = hdf5_getters.hdf5_open_readonly(this.path + "/" + this.getTrackID() + ".h5");
        try {
            double pitch[] = hdf5_getters.get_segments_pitches(h5);
            double timbre[] = hdf5_getters.get_segments_timbre(h5);
            double loudness = hdf5_getters.get_loudness(h5);
            double tempo = hdf5_getters.get_tempo(h5);

            setSegmentPitches(pitch);
            setSegmentTimbres(timbre);
            printStatistics();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        hdf5_getters.hdf5_close(h5);
    }
    private void printStatistics() {
        printArray(this.segments_pitch_mean);
        printArray(this.segments_pitch_var);
        printArray(this.segments_timbre_mean);
        printArray(this.segments_timbre_var);
    }

    private void printArray(double[] array) {
        for (double d : array) {
            out.print(d + "\t");
        }
        out.print("\n");
    }
    private void setSegmentTimbres(double[] timbre) {
        ArrayList<Double[]> pitches = unconcatnate(timbre, 12);
        Iterator it = pitches.iterator();
        int count = 0;
        while (it.hasNext()) {
            Double[] column = (Double[]) it.next();
            double temp[] = statistics(column);
            this.segments_timbre_mean[count] = temp[0];
            this.segments_timbre_var[count] = temp[1];
            count++;
        }
    }
    private void setSegmentPitches(double[] pitch) {
        ArrayList<Double[]> pitches = unconcatnate(pitch, 12);
        Iterator it = pitches.iterator();
        int count = 0;
        while (it.hasNext()) {
            Double[] column = (Double[]) it.next();
            double temp[] = statistics(column);
            this.segments_pitch_mean[count] = temp[0];
            this.segments_pitch_var[count] = temp[1];
            count++;
        }
    }

    private ArrayList<Double[]> unconcatnate(double[] array, int numOfColumns) {
        ArrayList<Double[]> list = new ArrayList<Double[]>();
        int rows = array.length / numOfColumns;
        ArrayList<Double> tempList;
        for (int column = 0; column < 12; column++) {
            int row = 0;
            tempList = new ArrayList<Double>();
            while (row < rows) {
                tempList.add(array[row++ * 12 + column]);
            }
            list.add(tempList.toArray(new Double[0]));
        }
        return list;
    }
    private double[] statistics(Double[] nums) {
        int n = 0;
        double sum = 0;
        double sum_sqr = 0;
        for (double x : nums) {
            n = n + 1;
            sum += x;
            sum_sqr = sum_sqr + x * x;
        }
        double mean = sum / n;
        double variance = (sum_sqr - (sum * mean)) / n;
        return new double[]{mean, variance};
    }

    /**
     * @return the trackID
     */
    public String getTrackID() {
        return trackID;
    }

    /**
     * @param trackID the trackID to set
     */
    public void setTrackID(String trackID) {
        this.trackID = trackID;
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }
}
